<template>
	<view class="index padding_bar_height">
		<view class="bar_height"></view>
		<!-- 首页顶部 -->
		<personal-center :banner-list="bannerList" :hasNew.sync="hasNew"></personal-center>
		<!-- 首页nav -->
		<index-nav></index-nav>
		<!-- 首页天气卡片 -->
		<weather-card :address="address" :weather="weather"></weather-card>
		<!-- 园区热点 -->
		<park-hot-spot :tab-list="hotSpotList"></park-hot-spot>
	</view>
</template>

<script>
	import PersonalCenter from '@/components/index/PersonalCenter.vue';
	import IndexNav from '@/components/index/IndexNav.vue';
	import WeatherCard from '@/components/index/WeatherCard.vue';
	import ParkHotSpot from '@/components/index/ParkHotSpot.vue';
	import { Index } from '@/api/index.js';
	import { mapMutations, mapState } from 'vuex';
	import jwt_decode from "jwt-decode";
	import { PasswordFreeLogin } from '@/utils/DDlogin.js';
	export default {
		data() {
			return {
				address: '',
				hotSpotList: [],
				bannerList: [],
				weather: {},
				hasNew: false
			}
		},
		components: {
			PersonalCenter,
			IndexNav,
			WeatherCard,
			ParkHotSpot
		},
		async onLoad() {
			// 免密登录
			// #ifdef MP-ALIPAY
			// if (!Object.keys(this.userInfo).length) {
				await this.PasswordFreeLogin();
			// }
			// #endif
			this.getXmlData();
			uni.setNavigationBarColor({
				backgroundColor: "#CED9F4"
			})
		},
		computed: {
			...mapState(['userInfo', 'flag'])
		},
		methods: {
			PasswordFreeLogin,
			...mapMutations(['changeAddressLng', 'changeAddressLat', 'changeAddressCity', 'changeUserInfo']),
			getGPSStatus() {
				if(plus.os.name == "Android"){
					var context = plus.android.importClass("android.content.Context");
					var locationManager=plus.android.importClass("android.location.LocationManager");
					var main=plus.android.runtimeMainActivity();
					var mainSvr=main.getSystemService(context.LOCATION_SERVICE);
					var gpsProvider = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);//检查是否开启了GPS
					if(!gpsProvider) {
						var message = "为了获取您的精准位置，请开启GPS设备。";
						var title = "GPS未启用";
						var alertCB = function () {
						var Intent = plus.android.importClass("android.content.Intent");
						var mIntent = new Intent('android.settings.LOCATION_SOURCE_SETTINGS');
						main.startActivity(mIntent);//打开GPS设置
						}
					plus.nativeUI.alert( message, alertCB, title);
					}
				}
			},
			getXmlData() {
				// #ifdef APP-PLUS
				this.getGPSStatus();
				// #endif
				this.$showLoading('加载中');
				const list = [
						Index.getBanner(),
						Index.getHotSpotType(),
						Index.getMessageNew(),
						// #ifndef H5
						uni.getLocation({ type: 'gcj02', geocode: true })
						// #endif
					];
					// 请求同时获取 定位 轮播 和热点分类数据
				Promise.all(list).then(async (res) => {
					this.bannerList = res[0].data.list;
					this.hotSpotList = res[1].data.list;
					this.hasNew = res[2].data.hasnew;
					// #ifdef APP-PLUS 
					this.address = res[3][1].address.city;
					this.changeAddressCity(res[3][1].address.city);
					// #endif
					// #ifndef APP-PLUS || H5 
					this.address = res[3][1].city;
					this.changeAddressCity(res[3][1].city);
					// #endif
					this.changeAddressLng(res[3][1].longitude);
					this.changeAddressLat(res[3][1].latitude);
					// 请求成功后根据天气获取定位数据
					const { data } = await Index.getWeather({ lng: res[3][1].longitude, lat: res[3][1].latitude });
					this.weather = data.weather;
					uni.hideLoading();
				}).catch((e) => {
					uni.hideLoading();
				});
			}
		},
		async onShow(){
			const res = await Index.getMessageNew()
			this.hasNew = res.data.hasnew
			console.log(res,'res')
			 
		}
	}
</script>

<style lang="scss" scoped>
</style>
